package com.cryrabbit.yyystitp.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cryrabbit.yyystitp.R;
import com.cryrabbit.yyystitp.model.Tiezi;

import java.util.List;

/**
 * Created by CryRabbit on 2016/9/17.
 */

public class MyRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private List<Tiezi> list;
    private static final int TYPE_ITEM=0;
    private static final int TYPE_FOOTER = 1;
    public MyRecycleAdapter(List<Tiezi> list) {
        this.list=list;
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1==getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_ITEM;
        }
    }
    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_ITEM){
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item,null);
            view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.setOnClickListener(this);
            return new ItemViewHolder(view);
        }else if(viewType==TYPE_FOOTER){
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_foot,null);
            view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new FooterViewHolder(view);
        }
        return null;
    }

    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(Tiezi)v.getTag());
        }
    }

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Tiezi data);
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder){
            ((ItemViewHolder)holder).content.setText(list.get(position).getContent()+"");
            ((ItemViewHolder)holder).user.setText(list.get(position).getUser()+"");
            String time=list.get(position).getTime().substring(8,10)+":"+list.get(position).getTime().substring(10,12);
            ((ItemViewHolder)holder).time.setText(time+"");
            //将数据保存在itemView的Tag中，以便点击时进行获取
            ((ItemViewHolder)holder).itemView.setTag(list.get(position));
        }
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    class FooterViewHolder extends RecyclerView.ViewHolder{
        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }
    class ItemViewHolder extends RecyclerView.ViewHolder{
        TextView user;
        TextView name;
        TextView answer;
        TextView content;
        TextView time;
        public ItemViewHolder(View view) {
            super(view);
            user=(TextView)view.findViewById(R.id.recycle_item_user);
            content=(TextView)view.findViewById(R.id.recycle_item_content);
            time=(TextView)view.findViewById(R.id.recycle_item_time);
            //name=(TextView)view.findViewById(R.id.recycle_item_name);
            //answer=(TextView)view.findViewById(R.id.recycle_item_answer);
        }
    }
}
